/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.lczyfy.idcp.modules.crawl.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lczyfy.idcp.modules.crawl.entity.CrawlLog;
import com.lczyfy.idcp.modules.crawl.service.CrawlLogService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.lczyfy.idcp.common.config.Global;
import com.lczyfy.idcp.common.persistence.Page;
import com.lczyfy.idcp.common.web.BaseController;
import com.lczyfy.idcp.common.utils.StringUtils;
import com.lczyfy.idcp.modules.crawl.entity.CrawlLog;
import com.lczyfy.idcp.modules.crawl.service.CrawlLogService;

/**
 * 日志记录Controller
 * @author cw
 * @version 2018-07-20
 */
@Controller
@RequestMapping(value = "${adminPath}/crawl/crawlLog")
public class CrawlLogController extends BaseController {

	@Autowired
	private CrawlLogService crawlLogService;
	
	@ModelAttribute
	public CrawlLog get(@RequestParam(required=false) String id) {
		CrawlLog entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = crawlLogService.get(id);
		}
		if (entity == null){
			entity = new CrawlLog();
		}
		return entity;
	}
	
	@RequiresPermissions("crawl:crawlLog:view")
	@RequestMapping(value = {"list", ""})
	public String list(CrawlLog crawlLog, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<CrawlLog> page = crawlLogService.findPage(new Page<CrawlLog>(request, response), crawlLog); 
		model.addAttribute("page", page);
		return "modules/crawl/crawlLogList";
	}

	@RequiresPermissions("crawl:crawlLog:view")
	@RequestMapping(value = "form")
	public String form(CrawlLog crawlLog, Model model) {
		model.addAttribute("crawlLog", crawlLog);
		return "modules/crawl/crawlLogForm";
	}

	@RequiresPermissions("crawl:crawlLog:edit")
	@RequestMapping(value = "save")
	public String save(CrawlLog crawlLog, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, crawlLog)){
			return form(crawlLog, model);
		}
		crawlLogService.save(crawlLog);
		addMessage(redirectAttributes, "保存日志记录成功");
		return "redirect:"+Global.getAdminPath()+"/crawl/crawlLog/?repage";
	}
	
	@RequiresPermissions("crawl:crawlLog:edit")
	@RequestMapping(value = "delete")
	public String delete(CrawlLog crawlLog, RedirectAttributes redirectAttributes) {
		crawlLogService.delete(crawlLog);
		addMessage(redirectAttributes, "删除日志记录成功");
		return "redirect:"+Global.getAdminPath()+"/crawl/crawlLog/?repage";
	}

}